#include <windows.h>
#include "DlgProc.h"
#include "FontPreviewProc.h"
#include "Common.h"
#include "resource.h"

static void FillFontList(HWND wnd);

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst,
				   PSTR cmdLine, int cmdShow)
{
	MSG msg;
	HWND dlgWnd;

	WNDCLASS wndclass;
	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = FontPreviewProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = inst;
	wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = TEXT("FontPreview");

	RegisterClass(&wndclass);

	dlgWnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_DLG), 0, DlgProc);

	FillFontList(dlgWnd);

	ShowWindow(dlgWnd, cmdShow);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

static int CALLBACK EnumFamCallBack(CONST LOGFONT *lf, CONST TEXTMETRIC *tm, DWORD fontType, LPARAM data) 
{
#if 0
	static const int PITCH_MASK = FIXED_PITCH | VARIABLE_PITCH;

	if ((lf->lfPitchAndFamily & ~PITCH_MASK) == FF_MODERN) // Is font monospaced?
#endif
	SendDlgItemMessage((HWND)data, IDC_FONTLIST, LB_ADDSTRING, 0, (LONG)lf->lfFaceName); 

	return 1;
}

static void FillFontList(HWND wnd)
{
	HDC dc;
	const char** fsp;

	// Fill font names list
	SendDlgItemMessage(wnd, IDC_FONTLIST, LB_RESETCONTENT, 0, 0); 
	dc = GetDC(wnd);
	EnumFonts(dc, 0, &EnumFamCallBack, (LPARAM)wnd);
	ReleaseDC(wnd, dc);

	SendDlgItemMessage(wnd, IDC_FONTLIST, LB_SETCURSEL, 0, 0);

	// Fill font sizes list
	for (fsp = fontSizes; *fsp != 0; ++fsp)
		SendDlgItemMessageA(wnd, IDC_FONTSIZELIST, LB_ADDSTRING, 0, (LPARAM)*fsp); 

	SendDlgItemMessage(wnd, IDC_FONTSIZELIST, LB_SETCURSEL, 4, 0);
}